<template>

  <!--
    作者：luoyiming
    时间：2020-06-04
    描述：插件中心-积分商城-商品设置-添加商品
  -->
  <div class="pb50">
    <el-form size="small" ref="form" :model="form" label-width="150px" v-if="!loading">
      <!--商品信息-->
      <Info></Info>

      <!--规格-->
      <Spec></Spec>

      <Other></Other>

      <!--提交-->
    </el-form>

    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
      <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
    </div>
  </div>
</template>

<script>
import BargainApi from '@/api/bargain';
import Info from './part/Info.vue';
import Spec from './part/Spec.vue';
import Other from './part/Other.vue';
import { formatModel } from '@/utils/base.js';
export default {
  components: {
    /*产品基本信息*/
    Info,
    /*规格*/
    Spec,
    /*其它*/
    Other
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表单*/
      form: {
        /*商品ID*/
        productId: 0,
        /*活动ID*/
        bargainActivityId: 0,
        /*商品状态*/
        status: 10,
        /*商品名称*/
        productName: '',
        /*规格*/
        productSkuId: 0,
        /*商品表格*/
        tableData: [],
        /*活动名称*/
        activeName: '',
        limitNum: ''
      }
    };
  },
  provide: function () {
    return {
      form: this.form,
      type: 'add'
    };
  },
  created() {
    if (this.$route.query.productId != null) {
      this.form.productId = this.$route.query.productId;
      this.form.bargainActivityId = this.$route.query.bargainActivityId;
      this.getData();
    }
  },
  methods: {
    /*获取商品*/
    getData() {
      let self = this;
      BargainApi.getProduct({
        productId: self.form.productId,
        bargainActivityId: self.form.bargainActivityId
      }, true).then(res => {
        Object.assign(self.form, res.data);
        self.form.activeName = res.data.title;
        self.form.productName = res.data.product.productName;
        self.form.productImage = res.data.product.imagePath;
        self.creatSpec();
        self.loading = false;
      });
    },
    /*格式规格表格数据*/
    creatSpec() {
      let self = this;
      let obj = {
        productName: self.form.product.productName,
        productId: self.form.product.productId,
        productPrice: self.form.product.productPrice,
        stockNum: self.form.product.productStock,
        productSkuId: self.form.product.sku[0].productSkuId,
        specType: self.form.product.specType,
        bargainPrice: 0,
        bargainStock: 0,
        bargainNum: 0,
        tempProduct: true,
        bargainProductId: 0,
        bargainProductSkuId: 0
      };
      self.form.tableData.push(obj);
    },
    /*提交表单*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate(valid => {
        if (valid) {
          let params = {
            product: {}
          };
          params.productId = self.form.productId;
          params.bargainActivityId = self.form.bargainActivityId;
          params.limitNum = self.form.limitNum;
          params.bargainNum = self.form.bargainNum;
          params.specList = self.tableFormet(self.form.tableData);
          self.loading = true;
          BargainApi.addProduct(params, true).then(data => {
            self.loading = false;
            ElMessage({
              message: "恭喜你，添加成功",
              type: 'success'
            });
            self.cancelFunc();
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*表格数据格式化*/
    tableFormet(list) {
      list.forEach(item => {
        item.productAttr = item.specName != null ? item.specName : '';
      });
      return list;
    },
    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    }
  }
};
</script>

<style>
.edit_container {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>

